package com.xbongbong.pro.service.cool.app;

import com.alibaba.dingtalk.openapi.helper.DingCoolAppHelper;
import com.alibaba.dingtalk.openapi.helper.Env;
import com.aliyun.dingtalkim_1_0.models.InteractiveCardCreateInstanceRequest;
import com.aliyun.dingtalkim_1_0.models.PrivateDataValue;
import com.aliyun.dingtalkim_1_0.models.SendInteractiveCardRequest;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.coolapp.pojo.CardDataPojo;
import com.xbongbong.saas.domain.entity.CoolAppCardDataEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/08/12 15:19
 */
@Component
public class BizManagerHelp {

    @Resource
    private DingCoolAppHelper dingCoolAppHelper;
    @Resource
    private ProBaseConfig proBaseConfig;

    /**
     * 发送互动卡片
     *
     * @param accessToken
     * @param cardDataEntity
     * @param cardData
     * @return
     */
    public String sendCard(String accessToken, CoolAppCardDataEntity cardDataEntity, CardDataPojo cardData) {

        // 共公数据
        SendInteractiveCardRequest.SendInteractiveCardRequestCardData sendCardData = new SendInteractiveCardRequest.SendInteractiveCardRequestCardData();
        sendCardData.setCardParamMap(cardData.getCardParamMap());
        sendCardData.setCardMediaIdParamMap(cardData.getCardMediaIdParamMap());
        // 私有数据
        Map<String, PrivateDataValue> sendCardPrivateData = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (Objects.nonNull(cardData.getCardPrivateData())) {
            for (Map.Entry<String, Map<String, String>> entry : cardData.getCardPrivateData().entrySet()) {
                String key = entry.getKey();
                Map<String, String> value = entry.getValue();
                PrivateDataValue privateDataValue = new PrivateDataValue();
                privateDataValue.setCardParamMap(value);
                sendCardPrivateData.put(key, privateDataValue);
            }
        }
        // 接收人
        return dingCoolAppHelper.sendInteractiveCard(accessToken, cardDataEntity.getOpenConversationId(), cardDataEntity.getRobotCode(),
                cardDataEntity.getCardId(), Env.ROUTE_KEY_CARD_LIKE, cardDataEntity.getCardBizId(), sendCardData, sendCardPrivateData,
                null, cardData.getReceiverUserIdList(), null);
    }

    /***
     * 发送吊顶卡片
     *
     * @param: accessToken
     * @param: cardDataEntity
     * @param: cardData
     * @param: cardPrivateData
     * @throws
     * @return java.lang.String
     * @author hongxiao
     * @date 2022-06-13 10:27
     * @since
     * @version
     */
    public String sendTopCard(String accessToken, CoolAppCardDataEntity cardDataEntity, CardDataPojo cardData) {
        // 共公数据
        InteractiveCardCreateInstanceRequest.InteractiveCardCreateInstanceRequestCardData sendCardData = new InteractiveCardCreateInstanceRequest.InteractiveCardCreateInstanceRequestCardData();
        sendCardData.setCardParamMap(cardData.getCardParamMap());
        sendCardData.setCardMediaIdParamMap(cardData.getCardMediaIdParamMap());
        // 私有数据
        Map<String, PrivateDataValue> sendCardPrivateData = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (Objects.nonNull(cardData.getCardPrivateData())) {
            for (Map.Entry<String, Map<String, String>> entry : cardData.getCardPrivateData().entrySet()) {
                String key = entry.getKey();
                Map<String, String> value = entry.getValue();
                PrivateDataValue privateDataValue = new PrivateDataValue();
                privateDataValue.setCardParamMap(value);
                sendCardPrivateData.put(key, privateDataValue);
            }
        }
        List<String> receiverUserIdList = cardData.getReceiverUserIdList();
        // 创建实例
        String processQueryKey = dingCoolAppHelper.createInteractiveCardInstance(accessToken, cardDataEntity.getCardId(), cardDataEntity.getOpenConversationId(), cardDataEntity.getRobotCode(), Env.ROUTE_KEY_CLOSE_TOP_CARD, cardDataEntity.getCardBizId(), receiverUserIdList, sendCardData, sendCardPrivateData);
        if (StringUtil.isNotEmpty(processQueryKey)) {
            // 开启吊顶功能
            dingCoolAppHelper.interactiveCardTopBoxesOpen(accessToken, cardDataEntity.getOpenConversationId(),  proBaseConfig.getCoolAppCode(), cardDataEntity.getCardBizId(), receiverUserIdList);
        }

        return processQueryKey;
    }

}
